package com.example.taximeter.service.impl;


import com.example.taximeter.bean.*;
import com.example.taximeter.config.GlobalParam;
import com.example.taximeter.mapper.*;
import com.example.taximeter.service.ParamFileService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Date;


/**
 * 参数文件
 */
@Service
public class ParamFileServiceImpl implements ParamFileService {

    private ParamFileMapper paramFileMapper;
    private FileInfoMapper fileInfoMapper;
    private FactMapper factMapper;
    private PriceMapper priceMapper;
    private TaximeterTypeMapper taximeterTypeMapper;
    private UserMapper userMapper;
    private ParamFileLogMapper paramFileLogMapper;


    @Autowired
    public ParamFileServiceImpl(ParamFileMapper paramFileMapper,FileInfoMapper fileInfoMapper, FactMapper factMapper, PriceMapper priceMapper, TaximeterTypeMapper taximeterTypeMapper,UserMapper userMapper,ParamFileLogMapper paramFileLogMapper){
        this.paramFileMapper = paramFileMapper;
        this.fileInfoMapper = fileInfoMapper;
        this.factMapper = factMapper;
        this.priceMapper = priceMapper;
        this.taximeterTypeMapper = taximeterTypeMapper;
        this.userMapper = userMapper;
        this.paramFileLogMapper = paramFileLogMapper;
    }


    @Override
    public ReturnInfo findParamFileALL(Integer pageNum, Integer pageSize, ParamFileView paramFileView) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<ParamFileView> paramFileViews = paramFileMapper.findParamFileAll(paramFileView);
        PageInfo<ParamFileView> pageInfo = new PageInfo<>(paramFileViews);
        if(paramFileViews.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }

    @Override
    public String trafficCheck(ParamFile paramFile) {
        paramFile.setTraffictime(GlobalParam.getTime());
        if(paramFileMapper.trafficCheck(paramFile)){
            return "成功";
        }else {
            return "失败";
        }
    }

    @Override
    public String priceCheck(ParamFile paramFile) {
        paramFile.setPricetime(GlobalParam.getTime());
        if(paramFileMapper.priceCheck(paramFile)){
            return "成功";
        }else {
            return "失败";
        }
    }

    @Override
    public String metrologyCheck(ParamFile paramFile) {
        paramFile.setMetrologytime(GlobalParam.getTime());
        if(paramFileMapper.metrologyCheck(paramFile)){
            return "成功";
        }else {
            return "失败";
        }
    }

    @Override
    public String addParamFile(ParamFile paramFile){

            if(taximeterTypeMapper.findJjqById(paramFile.getJjqtypeid()).getFactid()!=paramFile.getFactid()){
                return "计价器和计价器厂商不匹配,请重新选择";
            }
            FileInfo fileInfo = fileInfoMapper.findfileByFileid(paramFile.getFileid());
            paramFile.setFilename(fileInfo.getFilename());
            paramFile.setNewbj(1);
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipal();
            User user1 = userMapper.findUserByUserid(user.getId());
            paramFile.setWriteuser(user1.getUsername());
            paramFile.setStime(GlobalParam.getTime());
            if (paramFileMapper.addParamfile(paramFile)) {
                paramFileMapper.updateNewbj(paramFile);
                return "成功";
            } else {
                return "失败";
            }

    }

    @Override
    public String updateParamFile(ParamFileView paramFileView) {

        if(taximeterTypeMapper.findTaximeterTypeByTaximeterType(paramFileView.getJjqtype()).get(0).getFactid()!=factMapper.findFactByFactname(paramFileView.getFactname()).get(0).getId()){
                    return "计价器和计价器厂商不匹配,请重新选择";
        }
        //因为是由名称查到id，这里是如果名称不唯一也可以找到一个id
        if(factMapper.findFactByFactname(paramFileView.getFactname())!=null&&factMapper.findFactByFactname(paramFileView.getFactname()).size()>0){
            Fact fact = factMapper.findFactByFactname(paramFileView.getFactname()).get(0);
            paramFileView.setFactid(fact.getId());
        }
        if(priceMapper.findPriceByPriceType(paramFileView.getPricetype())!=null&&priceMapper.findPriceByPriceType(paramFileView.getPricetype()).size()>0){
            Price price = priceMapper.findPriceByPriceType(paramFileView.getPricetype()).get(0);
            paramFileView.setPriceid(price.getId());
        }
        if(taximeterTypeMapper.findTaximeterTypeByTaximeterType(paramFileView.getJjqtype())!=null&&taximeterTypeMapper.findTaximeterTypeByTaximeterType(paramFileView.getJjqtype()).size()>0){
            TaximeterType taximeterType = taximeterTypeMapper.findTaximeterTypeByTaximeterType(paramFileView.getJjqtype()).get(0);
            paramFileView.setJjqtypeid(taximeterType.getId());
        }
            FileInfo fileInfo = fileInfoMapper.findfileByFileid(paramFileView.getFileid());
            paramFileView.setFilename(fileInfo.getFilename());
            paramFileView.setNewbj(1);
            paramFileView.setStime(GlobalParam.getTime());
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipal();
            User user1 = userMapper.findUserByUserid(user.getId());
            paramFileView.setWriteuser(user1.getUsername());
            if (paramFileMapper.updateParamFile(paramFileView)) {
                paramFileMapper.updateNewbjEdit(paramFileView);
                return "成功";
            } else {
                return "失败";
            }
            }


    @Override
    public String deleteParamfile(String id) {
        if(paramFileMapper.deleteParamfile(id)){
            return "成功";
        }else {
            return "失败";
        }
    }

    @Override
    public ReturnInfo findParamFileALLNotCheck(Integer pageNum, Integer pageSize) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<ParamFileView> paramFileViews = paramFileMapper.findParamFileAllNotCheck();
        PageInfo<ParamFileView> pageInfo = new PageInfo<>(paramFileViews);
        if(paramFileViews.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }


    @Override
    public ReturnInfo findParamFileALLChecked(Integer pageNum, Integer pageSize) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<ParamFileView> paramFileViews = paramFileMapper.findParamFileAllChecked();
        PageInfo<ParamFileView> pageInfo = new PageInfo<>(paramFileViews);
        if(paramFileViews.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }

    @Override
    public List<ParamFileView> findParamfileList() {
        return paramFileMapper.findParamfileList();
    }

    @Override
    public List<ParamFileView> findParamfileListFile() {
        return paramFileMapper.findParamfileListFile();
    }


    @Override
    public ReturnInfo findParamFileLogALL(Integer pageNum, Integer pageSize,ParamFileLog paramFileLog) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<ParamFileLog> paramFileLogs = paramFileLogMapper.findParamFileLogAll(paramFileLog);
        PageInfo<ParamFileLog> pageInfo = new PageInfo<>(paramFileLogs);
        if(paramFileLogs.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }


    @Override
    public String addParamFileLog(ParamFileLog paramFileLog){
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipal();
            User user1 = userMapper.findUserByUserid(user.getId());
            paramFileLog.setUsername(user1.getUsername());
            paramFileLog.setOpertime(GlobalParam.getTime());
            if (paramFileLogMapper.addParamfileLog(paramFileLog)) {
                return "成功";
            } else {
                return "失败";
            }

    }

    @Override
    public ParamFileLog findParamfileLog(Integer id) {
        return paramFileLogMapper.findParamfilelog(id);
    }
}
